﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Xml;

namespace LevelEditor.Document
{
    [Serializable]
    public class TileSet
    {

        #region Properties

        /// <summary>
        /// Filepath to image used for this tileset
        /// </summary>
        [XmlAttribute]
        public string ImageFileName { get; set; }

        /// <summary>
        /// Name to use to identify the image used for this tileset
        /// </summary>
        [XmlAttribute]
        public string ResourceName { get; set; }

        /// <summary>
        /// Size of tileset image in pixels
        /// </summary>
        public Size ImageSize { get; set; }

        /// <summary>
        /// Size of tiles in this tileset in pixels
        /// </summary>
        public Size TileSize { get; set; }

        /// <summary>
        /// Size of tileset in tiles
        /// </summary>
        public Size Size { get; set; }

        /// <summary>
        /// list of tile definitions contained in this tileset
        /// </summary>
        [XmlArray]
        public List<TileDefinition> TileDefinitions { get; set; }

        /// <summary>
        /// Image for this tileset
        /// </summary>
        [XmlIgnore]
        public Image Image { get; set; }

        #endregion

        #region Constructors

        public TileSet()
        {
        }

        public TileSet(TileSet copy)
        {
            ImageFileName = copy.ImageFileName;
            ResourceName = copy.ResourceName;
            TileSize = copy.TileSize;
            ImageSize = copy.ImageSize;
            Size = copy.Size;
            TileDefinitions = new List<TileDefinition>(copy.TileDefinitions);
            if (copy.Image != null)
                Image = (copy.Image.Clone() as Image);
            else if (copy.ImageSize != Size.Empty)
                LoadImage();
        }

        #endregion

        #region Interface Methods

        public void LoadDefaults()
        {
            XmlReader reader = XmlReader.Create("Resources\\default.xml");
            XmlSerializer serializer = new XmlSerializer(typeof(TileSet));
            TileSet data = (serializer.Deserialize(reader) as TileSet);
            ImageFileName = data.ImageFileName;
            ResourceName = data.ResourceName;
            TileSize = data.TileSize;
            ImageSize = data.ImageSize;
            Size = data.Size;
            TileDefinitions = data.TileDefinitions;
            LoadImage();
            reader.Close();
        }

        public void LoadImage()
        {
            Image = Image.FromFile(ImageFileName);
        }

        public Rectangle GetSourceRectangleForTileValue(uint value)
        {
            return new Rectangle(((int)value % Size.Width) * TileSize.Width, 
                ((int)value / Size.Width) * TileSize.Height, TileSize.Width, TileSize.Height);
        }

        #endregion
    }
}
